package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Declaration;
import com.ruoyi.system.service.IDeclarationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报单数据管理Controller
 *
 * @author 海田精品馆
 * @date 2022-07-27
 */
@Controller
@RequestMapping("/system/declaration")
public class DeclarationController extends BaseController {
    private String prefix = "system/declaration";

    @Autowired
    private IDeclarationService declarationService;

    @RequiresPermissions("system:declaration:view")
    @GetMapping()
    public String declaration() {
        return prefix + "/declaration";
    }

    /**
     * 查询报单数据管理列表
     */
    @RequiresPermissions("system:declaration:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Declaration declaration) {
        startPage();
        List<Declaration> list = declarationService.selectDeclarationList(declaration);
        return getDataTable(list);
    }

    /**
     * 导出报单数据管理列表
     */
    @RequiresPermissions("system:declaration:export")
    @Log(title = "报单数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Declaration declaration) {
        List<Declaration> list = declarationService.selectDeclarationList(declaration);
        ExcelUtil<Declaration> util = new ExcelUtil<Declaration>(Declaration.class);
        return util.exportExcel(list, "报单数据管理数据");
    }

    /**
     * 新增报单数据管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存报单数据管理
     */
    @RequiresPermissions("system:declaration:add")
    @Log(title = "报单数据管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Declaration declaration) throws Exception {
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        declaration.setCreateBy(sysUser.getLoginName());
        return toAjax(declarationService.insertDeclaration(declaration));
    }

    /**
     * 修改报单数据管理
     */
    @RequiresPermissions("system:declaration:edit")
    @GetMapping("/edit/{decId}")
    public String edit(@PathVariable("decId") Long decId, ModelMap mmap) {
        Declaration decDO = declarationService.selectDeclarationByDecId(decId);
        if (StringUtils.equals(decDO.getDecStatus(), "已核销")
                ||StringUtils.equals(decDO.getDecStatus(), "已作废")) {
            throw new ServiceException("不允许编辑销" + decDO.getDecStatus() + "状态的报单!");
        }
        mmap.put("declaration", decDO);
        return prefix + "/edit";
    }

    /**
     * 修改保存报单数据管理
     */
    @RequiresPermissions("system:declaration:edit")
    @Log(title = "报单数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Declaration declaration) {

        Declaration decDO = declarationService.selectDeclarationByDecId(declaration.getDecId());
        if (StringUtils.equals(decDO.getDecStatus(), "已核销")
                ||StringUtils.equals(decDO.getDecStatus(), "已作废")) {
            throw new ServiceException("不允许编辑销" + decDO.getDecStatus() + "状态的报单!");
        }
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        declaration.setUpdateBy(sysUser.getLoginName());
        return toAjax(declarationService.updateDeclaration(declaration));
    }

    /**
     * 删除报单数据管理
     */
    @RequiresPermissions("system:declaration:remove")
    @Log(title = "报单数据管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(declarationService.deleteDeclarationByDecIds(ids));
    }


    /**
     * 报单数据管理作废
     */
    @RequiresPermissions("system:declaration:cancel")
    @Log(title = " 报单数据管理作废", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(String ids) {
        String[] strings = Convert.toStrArray(ids);
        Arrays.stream(strings)
                .filter(StringUtils::isNotBlank)
                .mapToLong(Long::parseLong)
                .forEach(id -> {
//                    Declaration decDO = declarationService.selectDeclarationByDecId(id);
//                    if(!StringUtils.equals(decDO.getDecStatus(),"已报单")){
//                        throw new ServiceException("不允许作废"+decDO.getDecStatus()+"状态的报单!");
//                    }

                    Declaration declaration = new Declaration();
                    declaration.setDecId(id);
                    declaration.setDecStatus("已作废");
                    SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
                    declaration.setUpdateBy(sysUser.getLoginName());
                    declarationService.updateDeclaration(declaration);
                });

        return toAjax(strings.length);
    }

    /**
     * 核销报单数据管理
     */
    @RequiresPermissions("system:declaration:chargeOff")
    @GetMapping("/chargeOff/{decId}")
    public String chargeOff(@PathVariable("decId") Long decId, ModelMap mmap) {
        Declaration declaration = declarationService.selectDeclarationByDecId(decId);
        mmap.put("declaration", declaration);
        return prefix + "/chargeOff";
    }

    /**
     * 报单数据管理核销
     */
    @RequiresPermissions("system:declaration:chargeOff")
    @Log(title = " 报单数据管理核销", businessType = BusinessType.UPDATE)
    @PostMapping("/chargeOff")
    @ResponseBody
    public AjaxResult chargeOff(Declaration declaration) {

        Declaration decDO = declarationService.selectDeclarationByDecId(declaration.getDecId());
        if (!StringUtils.equals(decDO.getDecStatus(), "已抽奖")
                &&!StringUtils.equals(decDO.getDecStatus(), "已报单")) {
            throw new ServiceException("不允许核销" + decDO.getDecStatus() + "状态的报单!");
        }
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        declaration.setUpdateBy(sysUser.getLoginName());
        return toAjax( declarationService.updateDeclaration(declaration));
    }

    /**
     * 报单数据管理核销
     */
    @RequiresPermissions("system:declaration:batchChargeOff")
    @Log(title = " 报单数据管理核销", businessType = BusinessType.UPDATE)
    @PostMapping("/chargeOffBatch")
    @ResponseBody
    public AjaxResult chargeOffBatch(String ids) {
        String[] strings = Convert.toStrArray(ids);
        Arrays.stream(strings)
                .filter(StringUtils::isNotBlank)
                .mapToLong(Long::parseLong)
                .forEach(id -> {
//                    Declaration decDO = declarationService.selectDeclarationByDecId(id);
//                    if(!StringUtils.equals(decDO.getDecStatus(),"已报单")){
//                        throw new ServiceException("不允许作废"+decDO.getDecStatus()+"状态的报单!");
//                    }
                    Declaration decDO = declarationService.selectDeclarationByDecId(id);
                    if (!StringUtils.equals(decDO.getDecStatus(), "已抽奖")
                            &&!StringUtils.equals(decDO.getDecStatus(), "已报单")) {
                        throw new ServiceException("不允许核销" + decDO.getDecStatus() + "状态的报单!");
                    }
                    Declaration declaration = new Declaration();
                    declaration.setDecId(id);
                    declaration.setDecStatus("已作废");
                    SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
                    declaration.setUpdateBy(sysUser.getLoginName());
                    declarationService.updateDeclaration(declaration);
                });

        return toAjax(strings.length);
    }

    /**
     * 再次维护报单数据管理
     */
    @RequiresPermissions("system:declaration:agentMaintain")
    @GetMapping("/agentMaintain/{decId}")
    public String agentMaintain(@PathVariable("decId") Long decId, ModelMap mmap) {
        Declaration declaration = declarationService.selectDeclarationByDecId(decId);
        mmap.put("declaration", declaration);
        return prefix + "/agentMaintain";
    }

    /**
     * 再次维护报单数据管理
     */
    @RequiresPermissions("system:declaration:agentMaintain")
    @Log(title = "再次维护报单数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/agentMaintain")
    @ResponseBody
    public AjaxResult agentMaintain(Declaration declaration) {
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        declaration.setUpdateBy(sysUser.getLoginName());
        return toAjax( declarationService.updateDeclaration(declaration));
    }

}















